package com.example.mystoryapp2.ui.story.page

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.StoryContext
import com.example.mystoryapp2.databinding.ActivityPageLayoutBinding
import com.example.mystoryapp2.domain.Page
import com.example.mystoryapp2.enums.EnumPageWorkMode
import com.example.mystoryapp2.ui.base.BaseActivity
import com.example.mystoryapp2.ui.base.BaseListAdapter
import com.example.mystoryapp2.ui.utils.DialogUtils
import com.example.mystoryapp2.ui.utils.RecycleViewUtils

class PageActivity : BaseActivity<ActivityPageLayoutBinding, PageViewModel>() {
    private val TAG = "PageActivity"

    companion object {
        /**
         * 下面的方法能启动不同模式下的本个Activity， 根据这些参数的不同会根据数据的参数显示不同模式的内容（读者，编辑等）
         */
        fun startReadInstance(ctx: Context, pageId: Int) {
            StoryContext.pageWorkMode = EnumPageWorkMode.READ
            StoryContext.pageId = pageId
            StoryContext.oriPageId = -1
            startActivity(ctx)
        }

        fun startEditInstance(ctx: Context, pageId: Int) {
            StoryContext.pageWorkMode = EnumPageWorkMode.EDIT
            StoryContext.pageId = pageId
            StoryContext.oriPageId = -1
            startActivity(ctx)
        }

        fun startCreateInstance(ctx: Context) {
            StoryContext.pageWorkMode = EnumPageWorkMode.EDIT
            StoryContext.pageId = -1
            StoryContext.oriPageId = -1
            startActivity(ctx)
        }

        private fun startActivity(ctx: Context) {
            val intent = Intent(ctx, PageActivity::class.java)
            ctx.startActivity(intent)
        }
    }

    private fun initData() {
        initPage()
        //initPageList()
    }

    override fun initView() {
        initRv()
        initWriteable()
        initBtnVisible()
        initBtnListener()
    }

    private fun initPage() {
        db.viewModel?.initPage {
            initPageList()
            initBtnVisible()
        }
    }

    private fun initRv() {
        RecycleViewUtils.addLLLayoutManager(this@PageActivity, db.rv)
    }

    private fun initWriteable() {
        db.tvContent.isEnabled = db.viewModel?.isRead() != true
        db.tvTitle.isEnabled = db.viewModel?.isRead() != true
    }

    private fun initBtnVisible() {
        if (db.viewModel?.isRead() == true) {
            db.btnNewPage.visibility = View.GONE
            db.btnSave.visibility = View.GONE
        } else {
            db.btnNewPage.visibility = View.VISIBLE
            db.btnSave.visibility = View.VISIBLE
        }

        if (db.viewModel?.isFirst() == true) {
            db.btnBack.visibility = View.GONE
        } else {
            db.btnBack.visibility = View.VISIBLE
        }
    }

    private fun initBtnListener() {
        db.btnSave.setOnClickListener {
            save{}
        }
        db.btnToStory.setOnClickListener {
            toStory()
        }
        db.btnNewPage.setOnClickListener {
            toNewPage()
        }
        db.btnBack.setOnClickListener { toParentPage() }
    }

    private fun clearScreen() {
        db.tvTitle.setText("")
        db.tvContent.setText("")
    }

    /**
     * 通过切换pageId 和originId 并且刷新界面 完成添加新页面 前进 后退等功能
     *  toParentPage()
     *  toNewPage()
     *  toChildPage(id: Int)
     */
    private fun toParentPage() {
        StoryContext.pageId = StoryContext.oriPageId
        refresh()
    }

    private fun toNewPage() {
        save {
            StoryContext.oriPageId = StoryContext.pageId
            StoryContext.pageId = -1
            clearScreen()
            refresh()
        }
    }

    private fun toChildPage(id: Int) {
        StoryContext.oriPageId = StoryContext.pageId
        StoryContext.pageId = id
        refresh()
    }

    private fun toStory() {
        finish()
    }

    private fun save(f: () -> Unit) {
        db.viewModel?.save {
            f()
            if (it) {
                DialogUtils.showLongMessage(this@PageActivity, R.string.tip_success)
            } else {
                DialogUtils.showLongMessage(this@PageActivity, R.string.tip_failed)
            }
        }
    }

    private fun initPageList() {
        db.viewModel?.initPageList(object : BaseListAdapter.IClickListener<Page> {
            override fun onClick(view: View, item: Page) {
                Log.d(TAG, "onClick: " + item.pageId!!)
                toChildPage(item.pageId!!)
            }
        })
    }

    private fun refresh() {
        db.viewModel?.initPage {
            initBtnVisible()
            initPageList()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_page_layout;
    }

    override fun getViewModel(): PageViewModel {
        return ViewModelProvider(
            this, ViewModelProvider
                .AndroidViewModelFactory(application)
        )[PageViewModel::class.java]
    }

    override fun setViewModel() {
        db.viewModel = getViewModel()
    }

    override fun onBackPressed() {

        if (db.viewModel?.isFirst() == true) {
            toStory()
        } else {
            toParentPage()
        }
    }
}